package com.beiing.tianshuai.tianshuai.mine.model;

import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.entity.CommonBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicPraiseBean;
import com.beiing.tianshuai.tianshuai.entity.FollowResultBean;
import com.beiing.tianshuai.tianshuai.entity.PersonalDynamicBean;
import com.beiing.tianshuai.tianshuai.entity.PersonalInfoBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.retrofit.ApiServiceManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/12/21 0021.
 */

public class NewPersonalIndexModel implements NewPersonalIndexModelImpl {

    private static final String TAG = "NewPersonalIndexModel";
    private OnPersonalIndexRequestListener mListener;

    public NewPersonalIndexModel(OnPersonalIndexRequestListener listener) {
        mListener = listener;
    }

    @Override
    public void getPersonalDynamicResult(final String uid, final String id, final int lastItemNum, final int pageSize) {
        Observable<PersonalDynamicBean> loginResult = ApiServiceManager.getPersonalDynamicsResult(
                HttpRequestConstant.MAIN_REQUEST_HEAD,
                uid,
                id,
                lastItemNum,
                pageSize
        );
        Observable<PersonalDynamicBean> signUpBeanObservable = loginResult.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<PersonalDynamicBean>() {

            @Override
            public void onNext(@NonNull PersonalDynamicBean bean) {
                if (mListener != null) {
                    mListener.getPersonalDynamicSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    getPersonalDynamicResult(uid, id, lastItemNum, pageSize);
                    mListener.getPersonalDynamicError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void getUserInfo(String uid, String toId) {
        Observable<PersonalInfoBean> result = DataManager.getPersonalInfoResult(
                uid,
                toId
        );
        Observable<PersonalInfoBean> signUpBeanObservable = result.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<PersonalInfoBean>() {

            @Override
            public void onNext(@NonNull PersonalInfoBean bean) {
                if (mListener != null) {
                    mListener.getPersonalInfoSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.getPersonalInfoError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void deleteRelay(String uid, String idOne, final int position) {
        Observable<CommonBean> loginResult = DataManager.getRelayDelResult(
                uid,
                idOne
        );
        Observable<CommonBean> signUpBeanObservable = loginResult.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<CommonBean>() {

            @Override
            public void onNext(@NonNull CommonBean bean) {
                LogUtils.i(bean.getCode() + bean.getData().getNumber(), TAG);
                if (mListener != null) {
                    mListener.onDelDynamicSuccess(bean, position);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mListener.onDelDynamicError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void deleteDynamic(String uid, String idOne, final int position) {
        Observable<CommonBean> loginResult = DataManager.getDynamicDeleteResult(
                uid,
                idOne
        );
        Observable<CommonBean> signUpBeanObservable = loginResult.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<CommonBean>() {

            @Override
            public void onNext(@NonNull CommonBean bean) {
                LogUtils.i(bean.getCode() + bean.getData().getNumber(), TAG);
                if (mListener != null) {
                    mListener.onDelDynamicSuccess(bean, position);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mListener.onDelDynamicError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void setLikeState(String uid, String aid, String toUid) {
        Observable<DynamicPraiseBean> loginResult = DataManager.getDynamicPraiseResult(
                uid,
                aid,
                toUid
        );
        Observable<DynamicPraiseBean> signUpBeanObservable = loginResult.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<DynamicPraiseBean>() {

            @Override
            public void onNext(@NonNull DynamicPraiseBean bean) {
                LogUtils.i(bean.getData().getNumber(), TAG);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void setFollowed(String uid, String toUid) {
        Observable<FollowResultBean> result = DataManager.getFollowResult(
                uid,
                toUid
        );
        Observable<FollowResultBean> signUpBeanObservable = result.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<FollowResultBean>() {

            @Override
            public void onNext(@NonNull FollowResultBean bean) {
                if (mListener != null) {
                    mListener.onFollowSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onFollowError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    public interface OnPersonalIndexRequestListener {

        void getPersonalInfoSuccess(PersonalInfoBean bean);

        void getPersonalInfoError(Throwable throwable);

        void getPersonalDynamicSuccess(PersonalDynamicBean bean);

        void getPersonalDynamicError(Throwable throwable);

        void onDelDynamicSuccess(CommonBean bean, int position);

        void onDelDynamicError(Throwable throwable);

        void onFollowSuccess(FollowResultBean bean);

        void onFollowError(Throwable throwable);
    }
}
